package com.jeecg.demo.controller;

import com.jeecg.demo.entity.*;
import com.jeecg.demo.service.BzjConfigServiceI;
import com.jeecg.demo.service.XmMessageServiceI;
import com.jeecg.demo.util.GetUserTenantId;
import com.jeecg.demo.util.PopupDataBindUtil;
import org.apache.commons.lang.StringUtils;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 *
 *
 */
@Controller
@RequestMapping("/bzjConfigController")
public class BzjConfigController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(BzjConfigController.class);

	@Autowired
	private BzjConfigServiceI bzjConfigServiceI;
	@Autowired
	private SystemService systemService;




	/**
	 *
	 *
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("com/jeecg/bzjconfig/bzjConfigList");
	}

	/**
	 * easyui AJAX请求数据
	 *
	 * @param request
	 * @param response
	 * @param dataGrid
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(BzjConfigEntity bzjConfigEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) throws Exception {
		CriteriaQuery cq = new CriteriaQuery(BzjConfigEntity.class, dataGrid);
		// 查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq,
				bzjConfigEntity, request.getParameterMap());
		try {
			// 自定义追加查询条件
		}
		catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}

		cq.add();
		this.bzjConfigServiceI.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);

	}

	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(BzjConfigEntity entity, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		entity = systemService.getEntity(BzjConfigEntity.class, entity.getId());
		message = "删除成功";
		try {
			bzjConfigServiceI.delete(entity);
			systemService.addLog(message, Globals.Log_Type_DEL,
					Globals.Log_Leavel_INFO);
		}
		catch (Exception e) {
			e.printStackTrace();
			message = "删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(BzjConfigEntity entity, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(entity.getId())) {
			entity = bzjConfigServiceI.getEntity(BzjConfigEntity.class, entity.getId());
			req.setAttribute("config", entity);
		}
		return new ModelAndView("com/jeecg/bzjconfig/bzjConfig-update");
	}

	/**
	 *
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(BzjConfigEntity entity, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "保证金配置更新成功";
		BzjConfigEntity t = bzjConfigServiceI.get(BzjConfigEntity.class, entity.getId());
		try {
			MyBeanUtils.copyBeanNotNull2Bean(entity, t);
			t.setUpdatetime(new Date());
			if (t.getMeans().equals("0")) {
                t.setRate(null);
			}else if (t.getMeans().equals("1")) {
				t.setAmount(null);
			}
			bzjConfigServiceI.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "保证金配置更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(BzjConfigEntity entity, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(entity.getId())) {
			entity = bzjConfigServiceI.getEntity(BzjConfigEntity.class, entity.getId());
			req.setAttribute("config", entity);
		}
		return new ModelAndView("com/jeecg/bzjconfig/bzjConfig-add");
	}

	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(BzjConfigEntity entity,
						  HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "添加成功";
		try {
			this.setModename(entity);

			List<BzjConfigEntity> hql = bzjConfigServiceI.findHql(" from BzjConfigEntity e where modeid = ? and modedirectory = ? ", entity.getModeid(), entity.getModedirectory());


			if (hql.size() > 0) {
				message = "已有该交易模式保证金配置信息";
				j.setMsg(message);
				return j;
			}
			if (entity.getMeans().equals("0") && entity.getAmount() == null) {
				message = "收取方式：固定保证金时，请填写【收取金额】";
				j.setMsg(message);
				return j;
			}
			if (entity.getMeans().equals("1") && entity.getAmount() == null) {
				message = "收取方式：比例保证金时，请选择【收取比例】";
				j.setMsg(message);
				return j;
			}

			List<BigDecimal> listbySql = bzjConfigServiceI.findListbySql("select max(id) from bzj_config");
			BigDecimal num = listbySql.get(0);
			entity.setId(num.intValue() + 1);
			entity.setCreatetime(new Date());
			if (entity.getMeans().equals("0")) {
				entity.setRate(null);
			}else if (entity.getMeans().equals("1")) {
				entity.setAmount(null);
			}
			bzjConfigServiceI.save(entity);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);

		}
		catch (Exception e) {
			e.printStackTrace();
			message = "添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	private void setModename(BzjConfigEntity entity) {
		if (entity.getModeid().equals("1")) {
			entity.setModeid("1");
			entity.setModename("挂牌销售");
			entity.setModedirectory("S");
		}else if (entity.getModeid().equals("2")) {
			entity.setModeid("1");
			entity.setModename("挂牌采购");
			entity.setModedirectory("B");
		}else if (entity.getModeid().equals("3")) {
			entity.setModeid("3");
			entity.setModename("竞价销售");
			entity.setModedirectory("S");
		}/*else if (entity.getModeid().equals("4")) {
			entity.setModeid("1");
			entity.setModename("竞价采购");
			entity.setModedirectory("B");
		}*/

	}

}
